﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class hotQuestionDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="hotQuestionID">标识列ID</param>
		public bool Exists(int hotQuestionID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [hotQuestion] where hotQuestionID=@hotquestionid ");
			htSyn.Add("@hotquestionid", hotQuestionID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<hotQuestion> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [reportTime],[hotQuestionID],[hotQuestionDescription],[hotQuestionTitle],[mark] FROM [hotQuestion]");
			 return gethotQuestionBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="hotQuestionID">标识列ID</param>
		/// <returns></returns>
		public hotQuestion GetModel(int hotquestionid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [reportTime],[hotQuestionID],[hotQuestionDescription],[hotQuestionTitle],[mark] FROM [hotQuestion]");
			strSql.Append(" where hotQuestionID=@hotquestionid ");
			htSyn.Add("@hotquestionid", hotquestionid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			hotQuestion hotquestionModel = new hotQuestion();
			if (dt.Rows.Count > 0)
			{
				hotquestionModel.reportTime=Convert.ToDateTime(dt.Rows[0]["reportTime"]);
				hotquestionModel.hotQuestionDescription=dt.Rows[0]["hotQuestionDescription"].ToString();
				hotquestionModel.hotQuestionTitle=dt.Rows[0]["hotQuestionTitle"].ToString();
				hotquestionModel.mark=dt.Rows[0]["mark"].ToString();
			}
			return hotquestionModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="hotQuestion">实体</param>
		/// <returns></returns>
		public bool Update(hotQuestion hotquestionModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [hotQuestion] set ");
			strSql.Append("[reportTime]=@reporttime,");
			strSql.Append("[hotQuestionDescription]=@hotquestiondescription,");
			strSql.Append("[hotQuestionTitle]=@hotquestiontitle,");
			strSql.Append("[mark]=@mark");

			strSql.Append(" where hotQuestionID=@hotquestionid ");
			htSyn.Add("@reporttime", hotquestionModel.reportTime);
			htSyn.Add("@hotquestionid", hotquestionModel.hotQuestionID);
			htSyn.Add("@hotquestiondescription", hotquestionModel.hotQuestionDescription);
			htSyn.Add("@hotquestiontitle", hotquestionModel.hotQuestionTitle);
			htSyn.Add("@mark", hotquestionModel.mark);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="hotQuestionID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int hotQuestionID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [hotQuestion] where hotQuestionID=@hotquestionid");
			htSyn.Add("hotquestionid", hotQuestionID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="hotQuestion">实体</param>
		/// <returns></returns>
		public bool Insert(hotQuestion hotquestionModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [hotQuestion]([reportTime],[hotQuestionDescription],[hotQuestionTitle],[mark]) values (@reporttime,@hotquestiondescription,@hotquestiontitle,@mark)");
			htSyn.Add("@reporttime", hotquestionModel.reportTime);
			htSyn.Add("@hotquestiondescription", hotquestionModel.hotQuestionDescription);
			htSyn.Add("@hotquestiontitle", hotquestionModel.hotQuestionTitle);
			htSyn.Add("@mark", hotquestionModel.mark);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<hotQuestion> gethotQuestionBySql(string strSql)
		{
			IList<hotQuestion> list = new List<hotQuestion>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					hotQuestion hotquestionModel = new hotQuestion();
					hotquestionModel.reportTime= Convert.ToDateTime(dr["reportTime"]);
					hotquestionModel.hotQuestionID= Convert.ToInt32(dr["hotQuestionID"]);
					hotquestionModel.hotQuestionDescription=dr["hotQuestionDescription"].ToString();
					hotquestionModel.hotQuestionTitle=dr["hotQuestionTitle"].ToString();
					hotquestionModel.mark=dr["mark"].ToString();
					list.Add(hotquestionModel);
				}
			}
			return list;
		}
		#endregion

        #region 热门问题分页
        /// <summary>
        /// 热门问题分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public DataTable pagingForHotQuestion(int pageIndex,int pageSize)
        {
            strSql.Remove(0, strSql.Length);
            return db.GetMPageDataTable(pageIndex, pageSize, "[reportTime],[hotQuestionID],[hotQuestionDescription],[hotQuestionTitle],[mark]", "hotQuestion", "", "reportTime", true);
        }
        #endregion

        #region 获取热门问题数量
        /// <summary>
        /// 获取热门问题数量
        /// </summary>
        /// <returns></returns>
        public int getRecordCount()
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            object objCount = null;
            strSql.Append("select count(1) from [hotQuestion]");
            objCount = db.ExecuteScalar(strSql.ToString());
            int i = 0;
            if (objCount!=null)
            {
                i = Convert.ToInt32(objCount);
            }
            return i;
        }         
        #endregion
    }
}
